<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Keepalived+Nginx高可用, 博客 blog">
    <meta name="description" content="熊猫小二的博客  xmxe&#39;s blog">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- 为了引用qq空间图床文件 -->
    <meta name="referrer" content="no-referrer">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Keepalived+Nginx高可用 | 熊猫小二</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/blog/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/my.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/loading.css">

    <script src="/blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>



   
<style>
    body{
       background-image: url(/blog/medias/cover.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    
  <div id="loading-box">
    <div class="loading-left-bg"></div>
    <div class="loading-right-bg"></div>
    <div class="spinner-box">
      <div class="configure-border-1">
        <div class="configure-core"></div>
      </div>
      <div class="configure-border-2">
        <div class="configure-core"></div>
      </div>
      <div class="loading-word">加载中...</div>
    </div>
  </div>
  <!-- 页面加载动画 -->
  <script>
    $(document).ready(function () {
      // document.body.style.overflow = 'auto';
      document.getElementById('loading-box').classList.add("loaded")
    })
  </script>

    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/blog/" class="waves-effect waves-light">
                    
                        <img src="/blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">熊猫小二</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-book" style="zoom: 0.6;"></i>
      
      <span>归档</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>标签</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/about">
          
          <i class="fas fa-star-of-david" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>主页</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/gallery">
          
          <i class="fas fa-images" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>相册</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  
    <li>
      <a class="waves-effect waves-light" onclick="switchNightMode()">
        <i id="sum-moon-icon" class="fas fa-sun" style="zoom:0.65;" title="切换主题"></i>
      </a>
    </li>
  
  
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
          <img src="/blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">熊猫小二</div>
        <div class="logo-desc">
            
            熊猫小二的博客  xmxe&#39;s blog
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book"></i>
			
			归档
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
		          <span>标签</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-star-of-david" style="position: absolute;left:50px" ></i>
			      
		          <span>主页</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/gallery " style="margin-left:75px">
				  
				   <i class="fa fas fa-images" style="position: absolute;left:50px" ></i>
			      
		          <span>相册</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/blog/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/blog/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/blog/medias/featureimages/8.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 id="post-title" class="description center-align post-title"></h1>

                    
                        <!-- <script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11"></script> -->
                        <script>
                            var typedObj = new Typed("#post-title", {
                                strings: [ 'Keepalived+Nginx高可用' ],
                                startDelay: 300,
                                typeSpeed: 70,
                                loop: false,
                                backSpeed: 50,
                                showCursor: true
                            });
                        </script>
                    
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
        background-color: rgb(255, 255, 255,0.7);
        border-radius: 10px;
        box-shadow: 0 10px 35px 2px rgba(0, 0, 0, .15), 0 5px 15px rgba(0, 0, 0, .07), 0 2px 5px -5px rgba(0, 0, 0, .1) !important;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
        max-height: 480px;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/blog/tags/%E5%AE%89%E8%A3%85/">
                                <span class="chip bg-color">安装</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/blog/libs/prism/prism.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="前期准备"><a href="#前期准备" class="headerlink" title="前期准备"></a>前期准备</h2><h3 id="服务器"><a href="#服务器" class="headerlink" title="服务器"></a>服务器</h3><table>
<thead>
<tr>
<th align="center">服务器</th>
<th align="center">CPU</th>
<th align="center">内存</th>
<th align="center">系统</th>
<th align="center">硬盘</th>
<th align="center">IP</th>
</tr>
</thead>
<tbody><tr>
<td align="center">MASTER&#x2F;NGINX服务器</td>
<td align="center">8核心、主频2.2GHz 以上</td>
<td align="center">1G</td>
<td align="center">Centos7</td>
<td align="center">20G</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">BACKUP&#x2F;NGINX服务器</td>
<td align="center">8核心、主频2.2GHz 以上</td>
<td align="center">1G</td>
<td align="center">Centos7</td>
<td align="center">20G</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">省调服务器（主）</td>
<td align="center">8核心、主频2.2GHz 以上</td>
<td align="center">8G</td>
<td align="center">Windows server2012</td>
<td align="center">20G</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">省调服务器（备）</td>
<td align="center">8核心、主频2.2GHz 以上</td>
<td align="center">8G</td>
<td align="center">Windows server2012</td>
<td align="center">20G</td>
<td align="center"></td>
</tr>
</tbody></table>
<h3 id="安装包"><a href="#安装包" class="headerlink" title="安装包"></a>安装包</h3><table>
<thead>
<tr>
<th align="center">nginx-1.8.1.tar.gz</th>
<th align="center">openssl-fips-2.0.16.tar.gz</th>
<th align="center">pcre-8.3.9.tar.gz</th>
<th align="center">zlib-1.2.11.tar.gz</th>
</tr>
</thead>
</table>
<h3 id="FTP工具"><a href="#FTP工具" class="headerlink" title="FTP工具"></a>FTP工具</h3><p>FileZilla</p>
<h3 id="流程图"><a href="#流程图" class="headerlink" title="流程图"></a>流程图</h3><p><img src="https://s21.ax1x.com/2024/07/01/pkcbfw4.jpg" alt="流程图"></p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><h3 id="安装nginx"><a href="#安装nginx" class="headerlink" title="安装nginx"></a>安装nginx</h3><p>上传解压，用Filezilla工具将nginx-1.8.1.tar.gz、opensssl-fips-2.0.16.tar.gz、pcre-8.3.9.tar.gz、安装包上传至服务器&#x2F;usr&#x2F;local&#x2F;wypt目录下并解压。编译安装如下</p>
<p>openssl：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token function">tar</span> zxvf openssl-fips-2.0.16.tar.gz
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token builtin class-name">cd</span> openssl-fips-2.0.16
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> ./config <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>pcre:</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token function">tar</span> zxvf pcre-8.39.tar.gz
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token builtin class-name">cd</span> pcre-8.39
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span>  ./configure <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>zlib:</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token function">tar</span> zxvf zlib-1.2.11.tar.gz
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token builtin class-name">cd</span> zlib-1.2.11
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> ./configure <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>最后安装nginx</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token function">tar</span> zxvf nginx-1.8.1.tar.gz
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token builtin class-name">cd</span> nginx-1.8.1
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> ./configure <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token operator">&amp;&amp;</span> <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<p>启动</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">/usr/local/nginx/sbin/nginx<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>启动成功后访问localhost:8888能正常打开nginx首页即安装成功</p>
<p>关闭</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">/usr/local/nginx/sbin/nginx <span class="token parameter variable">-s</span> stop <span class="token comment"># (立即停止，不保存相关信息)</span>
/usr/local/nginx/sbin/nginx <span class="token parameter variable">-s</span> quit <span class="token comment"># (正常退出，保存相关信息)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>重启</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">/usr/local/nginx/sbin/nginx <span class="token parameter variable">-s</span> reload<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h3 id="安装keepalived"><a href="#安装keepalived" class="headerlink" title="安装keepalived"></a>安装keepalived</h3><p>上传解压,用Filezilla工具将keepalived-1.1.20.tar.gz安装包上传至服务器&#x2F;usr&#x2F;local&#x2F;wypt目录下并解压，编译安装如下</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> <span class="token builtin class-name">cd</span> keepalived-1.1.20
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span> ./configure <span class="token parameter variable">--prefix</span><span class="token operator">=</span>/usr/local/keepalived
<span class="token punctuation">[</span>root@bogon keepalived-1.3.5<span class="token punctuation">]</span> <span class="token function">make</span>
<span class="token punctuation">[</span>root@bogon keepalived-1.3.5<span class="token punctuation">]</span> <span class="token function">make</span> <span class="token function">install</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>mkdir /etc/keepalived
<span class="token comment"># 将配置文件放到默认路径下</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
<span class="token comment"># keepalived主程序加入到环境变量（安装目录下）</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
<span class="token comment"># keepalived启动脚本变量引用文件，默认文件路径是/etc/sysconfig/</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
<span class="token comment"># keepalived启动脚本（源码目录下），放到/etc/init.d/目录下就可以使用service命令便捷调用</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>cd /etc/init.d/
<span class="token punctuation">[</span>root@init.d<span class="token punctuation">]</span>chmod +x keepalived
<span class="token comment">#添加服务</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>chkconfig <span class="token parameter variable">--add</span> keepalived
<span class="token comment">#开机启动</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>chkconfig keepalived on
<span class="token comment">#查看开机启动的服务</span>
<span class="token punctuation">[</span>root@keepalived<span class="token punctuation">]</span>chkconfig <span class="token parameter variable">--list</span>
<span class="token punctuation">[</span>root@init.d<span class="token punctuation">]</span>service keepalived start
Starting keepalived: /bin/bash: keepalived: <span class="token builtin class-name">command</span> not found
​<span class="token punctuation">[</span>FAILED<span class="token punctuation">]</span>

<span class="token punctuation">[</span>root@init.d<span class="token punctuation">]</span>ln <span class="token parameter variable">-s</span> /usr/local/keepalived/sbin/keepalived /usr/sbin/
<span class="token punctuation">[</span>root@init.d<span class="token punctuation">]</span>service keepalived start
Starting keepalived:                    <span class="token punctuation">[</span>  OK  <span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="相关配置"><a href="#相关配置" class="headerlink" title="相关配置"></a>相关配置</h2><h3 id="Nginx配置"><a href="#Nginx配置" class="headerlink" title="Nginx配置"></a>Nginx配置</h3><p>打开nginx配置文件<code>vim /usr/local/nginx/conf/nginx.conf</code>,编辑upstream模块及server中的location模块，端口为8888。其他的为默认配置,相关配置如下图</p>
<p><img src="https://s21.ax1x.com/2024/07/01/pkcq1nU.png" alt="配置"></p>
<h3 id="Keepalived配置"><a href="#Keepalived配置" class="headerlink" title="Keepalived配置"></a>Keepalived配置</h3><p><strong>MASTER配置</strong></p>
<p>打开keepalived配置文件<code>vim /etc/keepalived/keepalived.conf</code>编辑vrrp_script等模块，相关配置如下图</p>
<p><img src="https://s21.ax1x.com/2024/07/01/pkcqwjK.png" alt="配置"></p>
<p><strong>BACKUP配置</strong></p>
<p>相较于master配置，backup配置需要更改的地方有router_id,vrrp_instance VI_1模块下的state改为BACKUP，mcast_script_ip修改为本机的ip，priority修改为50，其他的与master一致，最终效果如下图</p>
<p><img src="https://s21.ax1x.com/2024/07/01/pkcqWgP.png" alt="配置"></p>
<h3 id="Nginx检测脚本"><a href="#Nginx检测脚本" class="headerlink" title="Nginx检测脚本"></a>Nginx检测脚本</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">vim</span> /etc/keepalived/nginx_check.sh

<span class="token comment">#!/bin/bash</span>
<span class="token assign-left variable">run</span><span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">ps</span> <span class="token parameter variable">-C</span> nginx --no-header <span class="token operator">|</span> <span class="token function">wc</span> <span class="token parameter variable">-l</span><span class="token variable">`</span></span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$run</span> <span class="token parameter variable">-eq</span> <span class="token number">0</span> <span class="token punctuation">]</span>
<span class="token keyword">then</span>
/usr/local/nginx/sbin/nginx <span class="token parameter variable">-s</span> stop
/usr/local/nginx/sbin/nginx
<span class="token function">sleep</span> <span class="token number">3</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable"><span class="token variable">`</span><span class="token function">ps</span> <span class="token parameter variable">-C</span> nginx --no-header <span class="token operator">|</span><span class="token function">wc</span> <span class="token parameter variable">-l</span><span class="token variable">`</span></span> <span class="token punctuation">]</span>
<span class="token keyword">then</span>
<span class="token function">killall</span> keepalived
<span class="token keyword">fi</span>
<span class="token keyword">fi</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>编辑完成后对脚本文件赋予权限<code>chmod +x nginx_check.sh</code>,一定需要赋予权限，否则无法执行此脚本导致keepalived开启服务的时候无法生成vip</p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><p>在master服务器上启动keepalived，<code>service keepalived start</code>通过<code>ip addr</code>查看成功开启虚拟ip192.168.32.254，此时访问192.168.32.254:8888可以正常访问，然后关掉master上的keepalived，模拟宕机，查看backup上的vip，虚拟ip已经由MASTER漂移到了BACKUP上，此时继续访问192.168.32.254:8888，依然可以访问，测试成功。</p>
<h2 id="安装过程中可能遇到的问题及解决办法"><a href="#安装过程中可能遇到的问题及解决办法" class="headerlink" title="安装过程中可能遇到的问题及解决办法"></a>安装过程中可能遇到的问题及解决办法</h2><p>问题一：安装nginx时报错configure: error: You need a C++ compiler for C++ support.[系统缺少c++环境]<br>可能原因：系统没有安装环境<br>解决办法:<br>情况1：当您的服务器能链接网络时候[联网安装gcc c++]</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span><span class="token comment"># yum install -y gcc gcc-c++</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>情况2：当您的服务器不能链接网络时候[不联网&#x2F;离线安装gcc c++],找到相关的安装包.我这里是挂载的系统安装盘.系统安装盘里面有相关的安装包,如果你没有安装盘在网上下载一下包也可以</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment">#挂载系统盘</span>
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span><span class="token comment"># mkdir -p /mnt/ROM</span>
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span><span class="token comment"># mount /dev/cdrom /mnt/ROM</span>
<span class="token comment">#切换到系统安装盘的Packages目录/mnt/ROM/Packages</span>
<span class="token punctuation">[</span>root@localhost<span class="token punctuation">]</span><span class="token comment">#cd /mnt/ROM/Packages</span>
<span class="token comment">#查看gcc相关安装包</span>
<span class="token punctuation">[</span>root@localhost Packages<span class="token punctuation">]</span><span class="token comment"># ls gcc*</span>
gcc-4.8.2-16.el7.x86_64.rpm  gcc-gfortran-4.8.2-16.el7.x86_64.rpm
gcc-go-4.8.2-16.el7.x86_64.rpm  gcc-objc++-4.8.2-16.el7.x86_64.rpm
gcc-c++-4.8.2-16.el7.x86_64.rpm  gcc-gnat-4.8.2-16.el7.x86_64.rpm
gcc-objc-4.8.2-16.el7.x86_64.rpm  gcc-plugin-devel-4.8.2-16.el7.x86_64.rpm
<span class="token comment">#安装gcc-c++即c++ compiler</span>
<span class="token punctuation">[</span>root@localhost Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh gcc-c++-4.8.2-16.el7.x86_64.rpm </span>
warning: gcc-c++-4.8.2-16.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 	f4a80eb5: NOKEY
error: Failed dependencies:
​	libstdc++-devel <span class="token operator">=</span> <span class="token number">4.8</span>.2-16.el7 is needed by gcc-c++-4.8.2-16.el7.x86_64
<span class="token comment">#安装失败,提示需要安装依赖包libstdc++-devel = 4.8.2-16.el7[版本号与您安装时候安装包相关],我们进行依赖包的查看及安装</span>
<span class="token punctuation">[</span>root@localhost Packages<span class="token punctuation">]</span><span class="token comment"># ll libstdc++-devel*</span>
-rw-rw-r--. <span class="token number">1</span> <span class="token number">500</span> <span class="token number">502</span> <span class="token number">1556984</span> Jul  <span class="token number">3</span>  <span class="token number">2014</span> libstdc++-devel-4.8.2-16.el7.i686.rpm
-rw-rw-r--. <span class="token number">1</span> <span class="token number">500</span> <span class="token number">502</span> <span class="token number">1561232</span> Jul  <span class="token number">3</span>  <span class="token number">2014</span> libstdc++-devel-4.8.2-16.el7.x86_64.rpm
<span class="token comment">#安装依赖包libstdc++-devel-4.8.2-16.el7.x86_64.rpm [版本号与您安装时候安装包相关]</span>
<span class="token punctuation">[</span>root@localhost Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh libstdc++-devel-4.8.2-16.el7.x86_64.rpm</span>
warning: libstdc++-devel-4.8.2-16.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, 	key ID f4a80eb5: NOKEY
Preparing<span class="token punctuation">..</span>.            <span class="token comment">################################# [100%]</span>
Updating / installing<span class="token punctuation">..</span>.
<span class="token number">1</span>:libstdc++-devel-4.8.2-16.el7   <span class="token comment">################################# [100%]</span>
<span class="token comment">#再次安装gcc-c++</span>
<span class="token punctuation">[</span>root@localhost Packages<span class="token punctuation">]</span><span class="token comment"># rpm -ivh gcc-c++-4.8.2-16.el7.x86_64.rpm </span>
warning: gcc-c++-4.8.2-16.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 	f4a80eb5: NOKEY
Preparing<span class="token punctuation">..</span>.            <span class="token comment">################################# [100%]</span>
Updating / installing<span class="token punctuation">..</span>.
  <span class="token number">1</span>:gcc-c++-4.8.2-16.el7      <span class="token comment">################################# [100%]</span>
<span class="token punctuation">[</span>root@localhost Packages<span class="token punctuation">]</span><span class="token comment">#</span>
<span class="token comment">#自此gcc-c++运行环境安装完成..</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>参考链接：<a target="_blank" rel="noopener" href="http://www.cnblogs.com/visec479/p/5315570.html">configure: error: You need a C++ compiler for C++ support.系统缺少c++环境</a></p>
<p>问题二：安装keepalived时也会报configure: error: Popt libraries is required相关错误<br>可能原因：系统缺少安装环境<br>解决办法：参考问题一<br>报错参考：Pkgconfig(libpcre) Download (RPM)<a target="_blank" rel="noopener" href="https://pkgs.org/download/pkgconfig(libpcre)">https://pkgs.org/download/pkgconfig(libpcre)</a></p>
<p>问题三：除本机外其他客户端无法访问ip<br>可能原因：防火墙开启或端口未开放<br>解决办法：关闭防火墙<code>systemctl stop firewalld</code>或者开放端口,添加<code>firewall-cmd --zone=public --add-port=8888/tcp --permanent</code>（–permanent永久生效，没有此参数重启后失效）重新载入<code>firewall-cmd --reload</code>查看<code>firewall-cmd --zone=public --query-port=8888/tcp</code>删除<code>firewall-cmd --zone=public --remove-port=8888/tcp --permanent</code></p>
<p>问题四：开启keepalived后主备服务器都有虚拟ip<br>可能原因：</p>
<ol>
<li>没有关闭selinux<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment">#临时关闭selinux</span>
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># getenforce</span>
Enforcing
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># setenforce 0</span>
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># getenforce</span>
Permissive
<span class="token comment">#永久关闭selinux：</span>
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token comment"># vim /etc/sysconfig/selinux</span>
<span class="token assign-left variable">SELINUX</span><span class="token operator">=</span>enforcing改为SELINUX<span class="token operator">=</span>disabled
<span class="token comment">#重启服务reboot</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li>
<li>VRRP协议问题<br>如果将SElinux关闭问题依旧存在，则可能是防火墙将MASTER的VRRP组播给挡住了。首先将防火墙关闭，确定防火墙是否为罪魁祸首。如果关闭防火墙后问题能够解决，我们只需要让VRRP组播其通过防火墙即可。主备都运行下面的命令<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">firewall-cmd <span class="token parameter variable">--direct</span> <span class="token parameter variable">--permanent</span> --add-rule ipv4 filter INPUT <span class="token number">0</span>  <span class="token parameter variable">--protocol</span> vrrp <span class="token parameter variable">-j</span> ACCEPT
firewall-cmd <span class="token parameter variable">--reload</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
参考连接<a target="_blank" rel="noopener" href="https://lyl-zsu.iteye.com/blog/2408296">Keepalived时主备负载均衡器都有VIP的问题：VRRP协议问题</a></li>
</ol>
<p>问题五：挂载系统时报错mount: no medium found on &#x2F;dev&#x2F;sr0<br>出现错误：没有介质在&#x2F;dev&#x2F;sr0找到<br>可能原因：未正确加载虚拟机，iso镜像并没有加载到虚拟机系统内<br>解决办法：在虚拟机的CD&#x2F;DVD设置里，将“已连接”和“打开电源时连接”两个选项，选中，确定即可。如下图所示</p>
<p>问题六：虚拟ip无法ping通，无法访问<br>可能原因：arp协议问题或vip网段设置不一致<br>解决办法：设置网段一致的vip</p>
<p>问题七：keepalived服务加入开机启动，但是开机自启动失败<br>可能原因：nginx没有实现开机自启动<br>由于keepalived开启服务的时候需要执行nginx检测脚本，nginx没有随着系统开机自启动，导致keepalived服务自启动失败<br>解决办法：将nginx做成系统服务实现开机自启动，或者在&#x2F;etc&#x2F;rc.d&#x2F;rc.local中加入启动命令&#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx<br>ps：&#x2F;etc&#x2F;rc.d&#x2F;rc.local是随着系统开机执行命令或脚本，但是系统默认此文件没有执行权限，需要chmod +x &#x2F;etc&#x2F;rc.d&#x2F;rc.local赋予权限，注释上也有</p>
<h2 id="相关文章"><a href="#相关文章" class="headerlink" title="相关文章"></a>相关文章</h2><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/lx0dT7bqd3mcIJCS83RdVg">Nginx挂了怎么办？怎么实现高可用？</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Re1CfTaw-NM0IsymRv2JwA">Nginx+keepalived实现高可用+防盗链+动静分离，写得太好了！</a></th>
<th align="center"></th>
</tr>
</thead>
</table>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/blog/about" rel="external nofollow noreferrer">xmxe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xmxe.github.io/blog/posts/ad742aa7ce3a/">https://xmxe.github.io/blog/posts/ad742aa7ce3a/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/blog/about" target="_blank">xmxe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/blog/tags/%E5%AE%89%E8%A3%85/">
                                    <span class="chip bg-color">安装</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        
                            <img src="/blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                        
                    </div>
                    <div id="wechat">
                        
                            <img src="/blog/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/blog/posts/92543cdfac32/">
                    <div class="card-image">
                        
                        <img src="https://img2.baidu.com/it/u=3204455731,2423486300&fm=253&fmt=auto&app=138&f=JPG?w=600&h=338" class="responsive-img" alt="FastDFS+Nginx安装">
                        
                        <span class="card-title">FastDFS+Nginx安装</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            从此网站下载相关压缩包，包括但不限于libfastcommon-master.zip、fastdfs-master.zip、fastdfs-nginx-module-master等
安装所需的依赖包yum install make cmak
                        

                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2024-07-01
                            -->

                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                
                                
                                <a href="/blog/tags/%E5%AE%89%E8%A3%85/">
                                    <span class="chip bg-color">安装</span>
                                </a>
                                
                                
                            

                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/blog/posts/2f3a45eead44/">
                    <div class="card-image">
                        
                        <img src="https://img2.baidu.com/it/u=746264043,112220562&fm=253&fmt=auto&app=138&f=JPEG?w=466&h=273" class="responsive-img" alt="MyCat读写分离搭建">
                        
                        <span class="card-title">MyCat读写分离搭建</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            前言mycat是最近很火的一款国人发明的分布式数据库中间件，它是基于阿里的cobar的基础上进行开发的,搭建之前我们先要配置mysql的主从复制，这个过程很长，我这里就不写了，有兴趣的可以看看我写的这篇文章。linux centos下mys
                        
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2024-07-01
                            -->
                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                
                                <a href="/blog/tags/%E5%AE%89%E8%A3%85/">
                                    <span class="chip bg-color">安装</span>
                                </a>
                                
                            
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="song"
                   id="569200213"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/blog/libs/aplayer/APlayer.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script> -->

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
            
                <span id="year">2022-2025
                </span>
            
            

            <a href="/blog/about" target="_blank">
                xmxe
            </a>
            |&nbsp;Powered by&nbsp;
            <a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>

            
            
            
            
            
            

            
            <br>

            
            <br>

            
        </div>
        <div class="col s12 m4 l4 social-link2 ">
    <a href="https://github.com/xmxe" class="tooltipped" target="_blank" data-tooltip="GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://gitee.com/xmxe" class="tooltipped" target="_blank" data-tooltip="码云" data-position="top" data-delay="50">
        <svg width="19" height="19" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="position: relative; top: 2px; left: 0.5px;">
            <path d="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z" fill="#fff">
            </path>
        </svg>
    </a>

















    
        
          <a href="/blog/download" class="tooltipped" target="_blank" data-tooltip="下载" data-position="top" data-delay="50">
            <i class="fas fa-download"></i>
          </a>
        
    



    <style>
  .mobiledevice {
    display: none !important;
  }

  footer .wechat_qrcode {
    position: fixed;
  }

  /*微信二维码*/
  .wechat_qrcode {
    position: absolute;
    margin-left: 10px;
    bottom: 10px;
    background: url("/blog/medias/xcx.png");
    zoom:40%;
  }

  .wechat:hover .wechat_qrcode {
    width: 430px;
    height: 430px;
    animation: move 0.4s linear 1 normal;
  }

  @keyframes move {
    0% {
      transform: translate(100px, 0);
      opacity: 0;
    }
    50% {
      transform: translate(50px, 0);
      opacity: 0.5;
    }
    100% {
      transform: translate(0, 0);
      opacity: 1;
    }
  }

  @media only screen and (max-width: 601px) {
    .wechat {
      display: none !important;
    }
    .mobiledevice {
      display: inline-block !important;
    }
  }
</style>

<a href="javascript:;" class="wechat" data-position="top" data-delay="50">
  <i class="fab fa-weixin"></i>
  <img class="wechat_qrcode" />
</a>

<a
  href="javascript:;"
  class="tooltipped mobiledevice"
  data-tooltip="微信: 464817304"
  data-position="top"
  data-delay="50"
>
  <i class="fab fa-weixin"></i>
</a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <div class="stars-con">
  <div id="stars"></div>
  <div id="stars2"></div>
  <div id="stars3"></div>  
</div>

<!-- 白天和黑夜主题 -->



<script>
  function switchNightMode() {
    
      setTimeout(function () {
        $('body').hasClass('DarkMode') 
        ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
        : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
          
        setTimeout(function () {
          $('.Cuteen_DarkSky').fadeOut(1e3, function () {
            $(this).remove()
          })
        }, 2e3)
      })
  }
</script>
    
    
    <script>
        /* 模式判断 */
        if (localStorage.getItem('isDark') === '1') {
            document.body.classList.add('DarkMode');
            $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')
        } else {
            document.body.classList.remove('DarkMode');
            $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')
        }
    </script>

    <script src="/blog/libs/materialize/materialize.min.js"></script>
    <script src="/blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/blog/libs/aos/aos.js"></script>
    <script src="/blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    

    

    

	
    

    

    
    <script type="text/javascript" src="/blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    

    <!-- 冒泡 -->
    
    <script type="text/javascript">
        // 只在桌面版网页启用特效
        // var windowWidth = $(window).width();
        
            document.write('<script type="text/javascript" src="/blog/libs/others/bubleAll.js"><\/script>');
        
        
    </script>
    

    <!-- 弹出文字 -->
    

</body>

</html>
